package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.FireManageInfor;
import com.yqg.protection.service.IFireManageInforService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * FireManageInforController 前端控制器
 *
 * @author zxf
 * @since 2024-10-16
 */
@RestController
@RequestMapping("/fire-manage-infor")
public class FireManageInforController {

    @Resource
    private IFireManageInforService fireManageInforService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者修改消防常识")
    public Result save(@RequestBody FireManageInfor fireManageInfor) {
        fireManageInfor.setIsDelete(0);
        return Result.success(fireManageInforService.saveOrUpdate(fireManageInfor));
    }

    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    @SysLog("删除消防常识")
    public Result delete(@PathVariable Integer id) {
        FireManageInfor fireManageInfor = fireManageInforService.getById(id);
        fireManageInfor.setIsDelete(1);
        return Result.success(fireManageInforService.saveOrUpdate(fireManageInfor));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<FireManageInfor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<FireManageInfor> list = fireManageInforService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(fireManageInforService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String keyWord) {
        QueryWrapper<FireManageInfor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.like("file_name", keyWord);
        queryWrapper.orderByDesc("id");
        return Result.success(fireManageInforService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String keyWord) {
        fireManageInforService.export(response, keyWord);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入消防常识")
    public void importData(MultipartFile file) {
        fireManageInforService.importData(file);
    }
}

